package com.example.demo.story.impl;

import com.example.demo.story.NetStoryServiceAbstract;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @Author TCJ
 * @create 2024/1/9
 */
public class BiQuGeImpl extends NetStoryServiceAbstract {
    @Override
    public String getBaseUrl() {
        return "http://www.biqugao.com";
    }

    @Override
    public String getReadUrl() {
        return getBaseUrl() + "/biquge/57153/";
    }

    @Override
    public void analysisChapter() {
        Document html = super.getHtml(getReadUrl());
        Element list = html.body().getElementsByClass("listmain").get(0);
        assert list != null;
        Elements dl = list.getElementsByTag("dl");
        boolean start =false;
        for (Element element : dl.get(0).children()) {
            if ("dt".equals(element.tag().getName())){
                if (element.text().contains("正文")){
                    start = true;
                    continue;
                }
            }
            if (start){
                Elements a = element.getElementsByTag("a");
                super.dto.getChapter().put(a.text(), getBaseUrl() +  a.attr("href"));
            }

        }
    }

    @Override
    public String getContent(String url) {
        StringBuilder sb = new StringBuilder();
        Document html = super.getHtml(url);
        Element content = html.body().getElementById("content");
        assert content != null;
        Elements p = content.getElementsByTag("p");
        for (Element element : p) {
            sb.append(element).append("\r\n");
        }
        return sb.toString();
    }
}
